{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "37a7abd2-b221-4bef-909d-ba64ad8fbe8b",
   "metadata": {},
   "source": [
    "# transforms 主要用途是对图像进行预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f68280f-c22d-410e-96cf-a3d958ff1a47",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "from torchvision import transforms\n",
    "import numpy as np\n",
    "from torch.utils.tensorboard import SummaryWriter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "244e0c18-4aec-48ac-94b3-607986d5ee9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "writer = SummaryWriter(\"20250425logs\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "794141ef-7076-4102-a4e8-64445ca0204d",
   "metadata": {},
   "source": [
    "**ToTensor 类**\n",
    "+ 将 PIL 图像或 NumPy ndarray 转换为 FloatTensor。\n",
    "+ 将图像的像素值从 [0, 255] 缩放到 [0.0, 1.0]。\n",
    "+ 将图像的通道从 HWC（高度、宽度、通道）格式转换为 CHW（通道、高度、宽度）格式。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aaef9359-5e83-41a9-a109-d7cde6edac6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_totensor = transforms.ToTensor()\n",
    "img_path = r\"D:\\desktop\\dog.jpg\"\n",
    "img = Image.open(img_path)\n",
    "img_tensor = trans_totensor(img)\n",
    "writer.add_image(\"ToTensor\",img_tensor,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4a6bd5c-a721-4329-89ae-39f02be4e7cf",
   "metadata": {},
   "source": [
    "**Normalize 类**\n",
    "\n",
    "`transforms.Normalize` 用于对图像数据进行标准化处理，公式为：\n",
    "$$\n",
    "\\mathrm{output}=\\frac{\\mathrm{input-mean}}{\\mathrm{std}}\n",
    "$$\n",
    "\n",
    "- `mean`：各通道的均值（需按RGB顺序传入）。\n",
    "- `std`：各通道的标准差。\n",
    "- `inplace`：是否原地修改数据，默认为 `False`。\n",
    "\n",
    "在使用 `transforms.Normalize` 函数之前，需要事先得到图像数据的均值和标准差。可以通过遍历整个数据集并计算每个通道的像素值的平均数和标准差来获取\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ebb1e247-383e-49c6-aa41-f1cc55b4edfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_nor = transforms.Normalize(mean=[2,4,6],std=[3,2,3])\n",
    "img_norm = trans_nor(img_tensor)\n",
    "writer.add_image(\"Normalize\",img_norm,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aad2270c-d28b-426e-bac3-9cfb4a8bbedd",
   "metadata": {},
   "source": [
    "**Resize 类**\n",
    "+ 将图像调整到指定的大小。\n",
    "+ 参数是一个元组，表示调整后的图像大小（高度、宽度）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e1b5c66b-6b22-437a-a362-08e3576ce766",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_resize = transforms.Resize((500,500))\n",
    "img_resize = trans_resize(img)\n",
    "writer.add_image(\"Resize\",trans_totensor(img_resize))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0104b48b-2e64-4ba1-8e7d-58ff65d6f309",
   "metadata": {},
   "source": [
    "**Compose 类**\n",
    "+ 将多个变换组合成一个序列。\n",
    "+ 参数是一个列表，包含多个变换操作实例。\n",
    "+ 这些变换操作按顺序依次应用于图像。\n",
    "\n",
    "**是一个固定的处理序列的方法**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a08b776f-6b4c-467f-b5ca-6277fc05b78e",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_resize_2 = transforms.Resize(512)\n",
    "trans_compose = transforms.Compose([trans_resize_2,trans_totensor])\n",
    "img_resize_2 = trans_compose(img)\n",
    "writer.add_image(\"Resize\",img_resize_2,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3663466-ff0f-4831-9217-ae9b84b2a1dd",
   "metadata": {},
   "source": [
    "**RandomCrop 类**\n",
    "+ 从图像中随机裁剪出一个指定大小的区域。\n",
    "+ 参数是一个元组，表示裁剪区域的大小（高度、宽度）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b0ecd31d-679b-4b4c-a09b-79aad3a75628",
   "metadata": {},
   "outputs": [],
   "source": [
    "trans_randcrop = transforms.RandomCrop((500,500))\n",
    "tran_compose_2 = transforms.Compose([trans_randcrop,trans_totensor])\n",
    "for i in range(10):\n",
    "    img_crop = tran_compose_2(img)\n",
    "    writer.add_image(\"RandomCrop\",img_crop,i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d6529efe-9cdd-40a4-8a1d-683c271fe14f",
   "metadata": {},
   "outputs": [],
   "source": [
    "writer.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93c1cce8-ea6c-4662-b97c-0e5972ce909c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
